﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;

namespace Docsbox.AccountFilterWebPart
{
    public partial class AccountFilterWebPartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                LoadDropDownListData();
                UpdateFieldsDataFromQueryString();               
            }
        }

        private void UpdateFieldsDataFromQueryString()
        {
            NameValueCollection collection = Page.Request.QueryString;
            foreach (string key in collection.AllKeys)
            {
                if(key.Contains("FilterField"))
                {
                    string valueKey = "FilterValue" + key.Substring(key.Length - 1);
                    string dataKey = collection[key];
                    if(dataKey.Equals("FinanceOffice"))
                    {
                        ddlBranch.SelectedValue = collection[valueKey];
                    }
                    if(dataKey.Equals("AccountStatus"))
                    {
                        ddlStatus.SelectedValue = collection[valueKey];
                    }
                    if(dataKey.Equals("FirstName"))
                    {
                        txtFirstname.Text = collection[valueKey];
                    }
                    if(dataKey.Equals("Title"))
                    {
                        txtDealNo.Text = collection[valueKey];
                    }
                    if(dataKey.Equals("RegistrationNumber"))
                    {
                        txtRegNo.Text = collection[valueKey];
                    }
                    if(dataKey.Equals("Surname"))
                    {
                        txtSurname.Text = collection[valueKey];
                    }
                    

                }
                //if(key.Contains("FilterMultiValue"))
                //{
                //    surname = collection["FilterMultiValue"].Trim('*');
                //    txtSurname.Text = surname;
                //}
            }

            //update the dropdown list selected item

            //update the text in the text fields
        }

        private void LoadDropDownListData()
        {
            SPWeb web = SPContext.Current.Web;
            //load branches
            SPList branches = web.Lists[Common.ListOffices];
            List<string> branchList = new List<string>();
            branchList.Add("All");
            foreach (SPListItem item in branches.Items)
            {
                string branch = Common.ConvertFieldToString(item["Title"], FieldType.Text);
                branchList.Add(branch);
            }
            ddlBranch.DataSource = branchList;
            ddlBranch.DataBind();

            //load statuses

            List<string> statusList = new List<string>();
            statusList.Add("All");

            SPList statuses = web.Lists[Common.ListAccountStatus];
            foreach (SPListItem item in statuses.Items)
            {
                string status = Common.ConvertFieldToString(item["Title"], FieldType.Text);
                statusList.Add(status);
            }
            ddlStatus.DataSource = statusList;
            ddlStatus.DataBind();

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //construct query string and post back
            Hashtable table = new Hashtable();
            string queryString = string.Empty;
            if(!ddlBranch.SelectedItem.Value.Equals("All"))
            {
                table.Add("FinanceOffice", ddlBranch.SelectedItem.Value);
            }
            if(!ddlStatus.SelectedItem.Value.Equals("All"))
            {
                table.Add("AccountStatus", ddlStatus.SelectedItem.Value);
            }
            if(!string.IsNullOrEmpty(txtFirstname.Text.Trim()))
            {
                table.Add("FirstName",txtFirstname.Text.Trim());
            }
            if(!string.IsNullOrEmpty(txtDealNo.Text.Trim()))
            {
                table.Add("Title",txtDealNo.Text.Trim());
            }
            if(!string.IsNullOrEmpty(txtRegNo.Text.Trim()))
            {
                table.Add("RegistrationNumber",txtRegNo.Text.Trim());
            }
            if(!string.IsNullOrEmpty(txtSurname.Text.Trim()))
            {
                table.Add("Surname",txtSurname.Text.Trim());
                //queryString += string.Format("FilterName=Surname&FilterMultiValue=*{0}*", txtSurname.Text.Trim());
            }

            //if (!String.IsNullOrEmpty(queryString))
            //    queryString += "&";

            int count = 1;
            foreach (var key in table.Keys)
            {
                queryString += string.Format("FilterField{0}={1}&FilterValue{0}={2}&", count, key, table[key]);
                count++;
            }
            queryString = queryString.TrimEnd('&');
            string postbackUrl = SPContext.Current.Web.Url +"/Lists/" + SPContext.Current.List + "/AllItems.aspx?" + queryString;
            HttpContext.Current.Response.Redirect(postbackUrl);
        }


    }
}
